<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Triangular Distribution</title>
<link rel="stylesheet" href="../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../dists.html" title="Distributions">
<link rel="prev" href="students_t_dist.html" title="Students t Distribution">
<link rel="next" href="uniform_dist.html" title="Uniform Distribution">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="students_t_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="uniform_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="math_toolkit.dist_ref.dists.triangular_dist"></a><a class="link" href="triangular_dist.html" title="Triangular Distribution">Triangular
        Distribution</a>
</h4></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">triangular</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
 <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RealType</span> <span class="special">=</span> <span class="keyword">double</span><span class="special">,</span>
           <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a>   <span class="special">=</span> <a class="link" href="../../pol_ref/pol_ref_ref.html" title="Policy Class Reference">policies::policy&lt;&gt;</a> <span class="special">&gt;</span>
 <span class="keyword">class</span> <span class="identifier">triangular_distribution</span><span class="special">;</span>

 <span class="keyword">typedef</span> <span class="identifier">triangular_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">triangular</span><span class="special">;</span>

 <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
 <span class="keyword">class</span> <span class="identifier">triangular_distribution</span>
 <span class="special">{</span>
 <span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">Policy</span>   <span class="identifier">policy_type</span><span class="special">;</span>

    <span class="identifier">triangular_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">lower</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">mode</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">upper</span> <span class="special">=</span> <span class="number">1</span><span class="special">);</span> <span class="comment">// Constructor.</span>
       <span class="special">:</span> <span class="identifier">m_lower</span><span class="special">(</span><span class="identifier">lower</span><span class="special">),</span> <span class="identifier">m_mode</span><span class="special">(</span><span class="identifier">mode</span><span class="special">),</span> <span class="identifier">m_upper</span><span class="special">(</span><span class="identifier">upper</span><span class="special">)</span> <span class="comment">// Default is -1, 0, +1 symmetric triangular distribution.</span>
    <span class="comment">// Accessor functions.</span>
    <span class="identifier">RealType</span> <span class="identifier">lower</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
    <span class="identifier">RealType</span> <span class="identifier">mode</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
    <span class="identifier">RealType</span> <span class="identifier">upper</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
 <span class="special">};</span> <span class="comment">// class triangular_distribution</span>

<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<p>
          The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
          distribution</a> is a <a href="http://en.wikipedia.org/wiki/Continuous_distribution" target="_top">continuous</a>
          <a href="http://en.wikipedia.org/wiki/Probability_distribution" target="_top">probability
          distribution</a> with a lower limit a, <a href="http://en.wikipedia.org/wiki/Mode_%28statistics%29" target="_top">mode
          c</a>, and upper limit b.
        </p>
<p>
          The triangular distribution is often used where the distribution is only
          vaguely known, but, like the <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29" target="_top">uniform
          distribution</a>, upper and limits are 'known', but a 'best guess',
          the mode or center point, is also added. It has been recommended as a
          <a href="https://www.jstor.org/stable/2988573" target="_top">proxy for the beta distribution.</a>
          The distribution is used in business decision making and project planning.
        </p>
<p>
          The <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">triangular
          distribution</a> is a distribution with the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
          density function</a>:
        </p>
<div class="blockquote"><blockquote class="blockquote"><p>
            <span class="serif_italic">f(x) = 2(x-a)/(b-a) (c-a)    for a &lt;= x &lt;=
            c</span>
          </p></blockquote></div>
<div class="blockquote"><blockquote class="blockquote"><p>
            <span class="serif_italic">f(x) = 2(b-x)/(b-a) (b-c)   for c &lt; x &lt;=
            b</span>
          </p></blockquote></div>
<p>
          Parameter <span class="emphasis"><em>a</em></span> (lower) can be any finite value. Parameter
          <span class="emphasis"><em>b</em></span> (upper) can be any finite value &gt; a (lower).
          Parameter <span class="emphasis"><em>c</em></span> (mode) a &lt;= c &lt;= b. This is the
          most probable value.
        </p>
<p>
          The <a href="http://en.wikipedia.org/wiki/Random_variate" target="_top">random variate</a>
          x must also be finite, and is supported lower &lt;= x &lt;= upper.
        </p>
<p>
          The triangular distribution may be appropriate when an assumption of a
          normal distribution is unjustified because uncertainty is caused by rounding
          and quantization from analog to digital conversion. Upper and lower limits
          are known, and the most probable value lies midway.
        </p>
<p>
          The distribution simplifies when the 'best guess' is either the lower or
          upper limit - a 90 degree angle triangle. The 001 triangular distribution
          which expresses an estimate that the lowest value is the most likely; for
          example, you believe that the next-day quoted delivery date is most likely
          (knowing that a quicker delivery is impossible - the postman only comes
          once a day), and that longer delays are decreasingly likely, and delivery
          is assumed to never take more than your upper limit.
        </p>
<p>
          The following graph illustrates how the <a href="http://en.wikipedia.org/wiki/Probability_density_function" target="_top">probability
          density function PDF</a> varies with the various parameters:
        </p>
<div class="blockquote"><blockquote class="blockquote"><p>
            <span class="inlinemediaobject"><img src="../../../../graphs/triangular_pdf.svg" align="middle"></span>

          </p></blockquote></div>
<p>
          and cumulative distribution function
        </p>
<div class="blockquote"><blockquote class="blockquote"><p>
            <span class="inlinemediaobject"><img src="../../../../graphs/triangular_cdf.svg" align="middle"></span>

          </p></blockquote></div>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h0"></a>
          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.member_functions"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.member_functions">Member
          Functions</a>
        </h5>
<pre class="programlisting"><span class="identifier">triangular_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">lower</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">mode</span> <span class="special">=</span> <span class="number">0</span> <span class="identifier">RealType</span> <span class="identifier">upper</span> <span class="special">=</span> <span class="number">1</span><span class="special">);</span>
</pre>
<p>
          Constructs a <a href="http://en.wikipedia.org/wiki/triangular_distribution" target="_top">triangular
          distribution</a> with lower <span class="emphasis"><em>lower</em></span> (a) and upper
          <span class="emphasis"><em>upper</em></span> (b).
        </p>
<p>
          Requires that the <span class="emphasis"><em>lower</em></span>, <span class="emphasis"><em>mode</em></span>
          and <span class="emphasis"><em>upper</em></span> parameters are all finite, otherwise calls
          <a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
        </p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top">
<p>
            These constructors are slightly different from the analogs provided by
            <a href="http://mathworld.wolfram.com" target="_top">Wolfram MathWorld</a>
            <a href="http://reference.wolfram.com/language/ref/TriangularDistribution.html" target="_top">Triangular
            distribution</a>, where
          </p>
<p>
            <code class="literal">TriangularDistribution[{min, max}]</code> represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical distribution
            giving values between min and max.<br> <code class="literal">TriangularDistribution[]</code>
            represents a <span class="bold"><strong>symmetric</strong></span> triangular statistical
            distribution giving values between 0 and 1.<br> <code class="literal">TriangularDistribution[{min,
            max}, c]</code> represents a triangular distribution with mode at
            c (usually <span class="bold"><strong>asymmetric</strong></span>).<br>
          </p>
<p>
            So, for example, to compute a variance using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
            Alpha</a>, use <code class="literal">N[variance[TriangularDistribution{1, +2}],
            50]</code>
          </p>
</td></tr>
</table></div>
<p>
          The parameters of a distribution can be obtained using these member functions:
        </p>
<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">lower</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
</pre>
<p>
          Returns the <span class="emphasis"><em>lower</em></span> parameter of this distribution (default
          -1).
        </p>
<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">mode</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
</pre>
<p>
          Returns the <span class="emphasis"><em>mode</em></span> parameter of this distribution (default
          0).
        </p>
<pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">upper</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span>
</pre>
<p>
          Returns the <span class="emphasis"><em>upper</em></span> parameter of this distribution (default+1).
        </p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h1"></a>
          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.non_member_accessors"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.non_member_accessors">Non-member
          Accessors</a>
        </h5>
<p>
          All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member accessor
          functions</a> that are generic to all distributions are supported:
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.hazard">Hazard Function</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.chf">Cumulative Hazard Function</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mean">mean</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.median">median</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mode">mode</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.variance">variance</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.skewness">skewness</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis">kurtosis</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis_excess">kurtosis_excess</a>,
          <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.range">range</a> and <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.support">support</a>.
        </p>
<p>
          The domain of the random variable is \lowerto \upper, and the supported
          range is lower &lt;= x &lt;= upper.
        </p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h2"></a>
          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.accuracy"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.accuracy">Accuracy</a>
        </h5>
<p>
          The triangular distribution is implemented with simple arithmetic operators
          and so should have errors within an epsilon or two, except quantiles with
          arguments nearing the extremes of zero and unity.
        </p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h3"></a>
          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.implementation"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.implementation">Implementation</a>
        </h5>
<p>
          In the following table, a is the <span class="emphasis"><em>lower</em></span> parameter of
          the distribution, c is the <span class="emphasis"><em>mode</em></span> parameter, b is the
          <span class="emphasis"><em>upper</em></span> parameter, <span class="emphasis"><em>x</em></span> is the random
          variate, <span class="emphasis"><em>p</em></span> is the probability and <span class="emphasis"><em>q = 1-p</em></span>.
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Function
                  </p>
                </th>
<th>
                  <p>
                    Implementation Notes
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    pdf
                  </p>
                </td>
<td>
                  <p>
                    Using the relation: pdf = 0 for x &lt; mode, 2(x-a)/(b-a)(c-a)
                    else 2*(b-x)/((b-a)(b-c))
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    cdf
                  </p>
                </td>
<td>
                  <p>
                    Using the relation: cdf = 0 for x &lt; mode (x-a)<sup>2</sup>/((b-a)(c-a))
                    else 1 - (b-x)<sup>2</sup>/((b-a)(b-c))
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    cdf complement
                  </p>
                </td>
<td>
                  <p>
                    Using the relation: q = 1 - p
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    quantile
                  </p>
                </td>
<td>
                  <p>
                    let p0 = (c-a)/(b-a) the point of inflection on the cdf, then
                    given probability p and q = 1-p:
                  </p>
                  <p>
                    x = sqrt((b-a)(c-a)p) + a ; for p &lt; p0
                  </p>
                  <p>
                    x = c ; for p == p0
                  </p>
                  <p>
                    x = b - sqrt((b-a)(b-c)q) ; for p &gt; p0
                  </p>
                  <p>
                    (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
                    for details.)
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    quantile from the complement
                  </p>
                </td>
<td>
                  <p>
                    As quantile (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
                    for details.)
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    mean
                  </p>
                </td>
<td>
                  <p>
                    (a + b + 3) / 3
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    variance
                  </p>
                </td>
<td>
                  <p>
                    (a<sup>2</sup>+b<sup>2</sup>+c<sup>2</sup> - ab - ac - bc)/18
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    mode
                  </p>
                </td>
<td>
                  <p>
                    c
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    skewness
                  </p>
                </td>
<td>
                  <p>
                    (See <a href="../../../../../../../boost/math/distributions/triangular.hpp" target="_top">/boost/math/distributions/triangular.hpp</a>
                    for details).
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    kurtosis
                  </p>
                </td>
<td>
                  <p>
                    12/5
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    kurtosis excess
                  </p>
                </td>
<td>
                  <p>
                    -3/5
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          Some 'known good' test values were obtained using <a href="http://www.wolframalpha.com/" target="_top">Wolfram
          Alpha</a>.
        </p>
<h5>
<a name="math_toolkit.dist_ref.dists.triangular_dist.h4"></a>
          <span class="phrase"><a name="math_toolkit.dist_ref.dists.triangular_dist.references"></a></span><a class="link" href="triangular_dist.html#math_toolkit.dist_ref.dists.triangular_dist.references">References</a>
        </h5>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              <a href="http://en.wikipedia.org/wiki/Triangular_distribution" target="_top">Wikipedia
              triangular distribution</a>
            </li>
<li class="listitem">
              <a href="http://mathworld.wolfram.com/TriangularDistribution.html" target="_top">Weisstein,
              Eric W. "Triangular Distribution." From MathWorld--A Wolfram
              Web Resource.</a>
            </li>
<li class="listitem">
              Evans, M.; Hastings, N.; and Peacock, B. "Triangular Distribution."
              Ch. 40 in Statistical Distributions, 3rd ed. New York: Wiley, pp. 187-188,
              2000, ISBN - 0471371246.
            </li>
<li class="listitem">
              <a href="http://www.measurement.sk/2002/S1/Wimmer2.pdf" target="_top">Gejza Wimmer,
              Viktor Witkovsky and Tomas Duby, Measurement Science Review, Volume
              2, Section 1, 2002, Proper Rounding Of The Measurement Results Under
              The Assumption Of Triangular Distribution.</a>
            </li>
</ul></div>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="students_t_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="uniform_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
